package com.mask.mk.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mask.mk.pojo.Goods;
import com.mask.mk.mapper.GoodsMapper;
import com.mask.mk.service.GoodsService;
import com.mask.mk.util.JsonResult;
import com.mask.mk.vo.goods.QueryByMultReq;
import com.mask.mk.vo.goods.UploadGoodsRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 商品信息 服务实现类
 * </p>
 *
 * @author joey
 * @since 2023-04-25
 */
@Service
public class GoodsServiceImpl  implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult insertUpdateGoods(UploadGoodsRequest uploadGoodsRequest) {

        JsonResult result=null;
//        Goods goods = goodsMapper.selectOne(null);
        Goods selectOne = goodsMapper.selectOne(Wrappers.<Goods>lambdaQuery()
                .and(wrapper -> wrapper.eq(Goods::getGid, uploadGoodsRequest.getGoodsId())));
        if(selectOne==null){ //新增
            Goods goods = new Goods();
            String name = uploadGoodsRequest.getFile().getOriginalFilename();
            goods.setGcode(uploadGoodsRequest.getGoodsCode())
                    .setTypeId(uploadGoodsRequest.getTypeId())
                    .setGname(uploadGoodsRequest.getGoodsName())
                    .setDetailName(uploadGoodsRequest.getDetailName())
                    .setGnum(uploadGoodsRequest.getGoodsNum())
                    .setGprice(uploadGoodsRequest.getGoodsPrice())
                    .setGstatus(uploadGoodsRequest.getGoodsStatus())
                    .setImgLink(name)
                    .setGdescribe(uploadGoodsRequest.getGoodsDescribe());
            int insert = goodsMapper.insert(goods);
            if(insert>0){
                result = JsonResult.success(goods);
            }else{
                result = JsonResult.fail("插入失败");
            }

        }else{ //修改
            System.out.println("修改");
//            Goods goods = new Goods();
            String name = uploadGoodsRequest.getFile().getOriginalFilename();
            selectOne.setGcode(uploadGoodsRequest.getGoodsCode())
                    .setTypeId(uploadGoodsRequest.getTypeId())
                    .setGname(uploadGoodsRequest.getGoodsName())
                    .setDetailName(uploadGoodsRequest.getDetailName())
                    .setGnum(uploadGoodsRequest.getGoodsNum())
                    .setGprice(uploadGoodsRequest.getGoodsPrice())
                    .setGstatus(uploadGoodsRequest.getGoodsStatus())
                    .setImgLink(name)
                    .setGdescribe(uploadGoodsRequest.getGoodsDescribe());
            int updateById = goodsMapper.updateById(selectOne);
            if(updateById>0){
                result = JsonResult.success(selectOne);
            }else{
                result = JsonResult.fail("修改失败");
            }
        }
        return result;
    }

    @Override
    public JsonResult getGoodsList() {
        List<Goods> goodsList = goodsMapper.selectList(null);
        JsonResult result=null;
        if(goodsList!=null){
            result = JsonResult.success(goodsList);
        }else{
            result = JsonResult.fail("商品列表为空");
        }
        return result;
    }

    @Override
    public JsonResult getGoodsById(int goodsId) {
        JsonResult result=null;
        Goods goods = goodsMapper.selectById(goodsId);
        if(goods!=null){
            result = JsonResult.success(goods);
        }else{
            result = JsonResult.fail("查询为空");
        }
        return result;
    }

    @Override
    public JsonResult getGoodsByMultipleConditions(UploadGoodsRequest ugReq) {
        JsonResult result=null;
        List<Goods> goodsList = goodsMapper.selectList(Wrappers.<Goods>lambdaQuery()
                .and(wrapper -> wrapper.eq(Goods::getTypeId, ugReq.getTypeId()))
                .or()
                .like(Goods::getGname, ugReq.getGoodsName())
                .or()
                .like(Goods::getDetailName, ugReq.getDetailName()));
        if(goodsList!=null){
            result = JsonResult.success(goodsList);
        }else{
            result = JsonResult.fail("查询为空");
        }
        return result;
    }

    public void getArry(){
        int arr2[] = new int[4];
        int arr1[] = new int[4];
            // 遍历数组2
            for (int i = 0; i < arr2.length; i++) {
                // 判断数组2中是否存在元素
                if (arr2[i] != 0) {
                    // 将数组2中的元素添加到数组1中对应的位置，如果数组1不够长，会抛出 ArrayIndexOutOfBoundsException 异常
                    arr1[i] = arr2[i];
                }
            }
            // 打印结果数组
            System.out.println(Arrays.toString(arr1));
    }

    @Override
    public JsonResult getGoodsByGroup(QueryByMultReq query) {


        int[] arr = new int[4];
        List<Integer> typeCondition = query.getTypeCondition();
        for (int i = 0; i < typeCondition.size(); i++) {
            arr[i] = typeCondition.get(i);
        }
//        int a1 = typeCondition.get(0);
//        int a2 = typeCondition.get(1);
//        int a3 = typeCondition.get(2);
//        int a4 = typeCondition.get(3);
        List<Goods> goodsList = goodsMapper.selectList(Wrappers.<Goods>lambdaQuery()
                .and(wrapper -> wrapper
                        .eq(Goods::getTypeId, arr[0])
                .or()
                .eq(Goods::getTypeId, arr[1]))
                .or()
                .eq(Goods::getTypeId, arr[2])
                .or()
                .eq(Goods::getTypeId, arr[3]));
        if(goodsList!=null){
            return JsonResult.success(goodsList);
        }else{
            return JsonResult.fail("查询失败");
        }
    }
}
